<?php
include_once '../includes/common.php';
if($islogin==1){}else sysmsg("您还未登录，请先登录",2,'./login.php',true);
$id=isset($_GET['id'])?intval($_GET['id']):sysmsg("参数错误",2,'./',true);
$row=$DB->get_row("SELECT * FROM authguao_program WHERE id='{$id}' limit 1");
if(!$row)sysmsg("程序不存在",2,'./prolist.php',true);
$title='程序安装包上传';
include_once './header.php';
//功能流程开始
include_once '../includes/pclzip.php';//zip压缩类
echo '<div class="row"><div class="col-lg-12"><div class="card"><div class="card-header">程序安装包(请打包安装包，系统会自动解压)</div><div class="card-body">';
if($_POST['s']==1){
    $extension=explode('.',$_FILES['file']['name']);
    if(($length=count($extension))>1){
        $ext=strtolower($extension[$length - 1]);
    }
    if($ext=='zip'){//判断是否是zip类 防止恶意上传
        echo "上传成功，正在解压中";
        copy($_FILES['file']['tmp_name'], ROOT.PACKAGE_DIR.'/'.$row['catalog'].'/release6000/release6000.zip');
        $zip = new ZipArchive;
        if($zip->open(ROOT.PACKAGE_DIR.'/'.$row['catalog'].'/release6000/release6000.zip') && $zip->extractTo(ROOT.PACKAGE_DIR.'/'.$row['catalog'].'/release6000/')){
            echo '<p>安装包解压成功</p>';
            $zip->close(ROOT.PACKAGE_DIR.'/'.$row['catalog'].'/release6000/release6000.zip');
            unlink(ROOT.PACKAGE_DIR.'/'.$row['catalog'].'/release6000/release6000.zip');
            exit ('<p>安装包已经自动删除</p>');
        }else{
            exit('<p>安装包解压失败</p>');
        }
    }else{
        echo "<font color=red>请打包好安装包再上传，系统会自动解压！</font>";
    }
}
//核心流程结束
echo '<form action="installer.php?id='.$id.'" method="POST" enctype="multipart/form-data"><label for="file"></label><input type="file" name="file" id="file"/><input type="hidden" name="s" value="1"/><div></br></div><input type="submit" class="btn btn-block btn-xs btn-outline-primary" value="确认上传"/><span class="btn btn-block btn-xs btn-outline-info" onclick="clean('.$id.');">清空原数据</a></form>';
echo '</div><div class="card-footer"><span class="layui-icon layui-icon-tips"></span> <font color=red>注意:上传之前请先清空原安装包数据<br></font><span class="layui-icon layui-icon-tips"></span> <font color=red>上传成功后，点击"<font color=blue>安装包设置</font>"更改安装信息</font></div></div></div></div>';
include_once './bottom.php';
?>
<script type="text/javascript">
function clean(id) {
    var ii = layer.msg('正在清除中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type : "POST",
        url : "ajax.php?act=installer_clean&id="+id,
        dataType : "json",
        success : function(data) {
            layer.close(ii);
            if(data.code == 0){
                layer.msg(data.msg, {icon: 6});
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
}
</script>